﻿////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// The MIT License (MIT)                                                      //
//                                                                            //
// Copyright (c) 2012 Lucas Teague and Charles Teague                         //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

// September 12, 2012

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace alarmclock
{
    public partial class alarm : Form
    {
        PadNumbericUpDown minuteUpDown = new PadNumbericUpDown();
        System.Media.SoundPlayer player = new System.Media.SoundPlayer();
        public alarm()
        {
            InitializeComponent();
            DateTime d = DateTime.Now;
            int h = d.Hour;
            string ampm = "am";
            if (h == 0) {
                h = 12;
            } else if (h >= 12) {
                if (h > 12)
                {
                    h -= 12;
                }
                ampm = "pm";
            }
            hourNumUpDown1.Value = h;
            minuteUpDown.Value = d.Minute;
            ampmCombo.Text = ampm;
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int interval = timeToRing();
            player.Stop();
            timer1.Interval = interval;
            timer1.Start();
        }

        /// <summary>
        /// Determine the number of seconds until the alarm clock should ring
        /// Lucas Teague and Charles Teague
        /// </summary>
        /// <returns>number of second till the clock rings</returns>
        private int timeToRing()
        {
            int hour = (int)hourNumUpDown1.Value;
            int minute = (int)minuteUpDown.Value;
            string ampm = ampmCombo.Text;
            if (ampm == "pm")
            {
                if (hour < 12)
                {
                    hour += 12;
                }
            }
            else if (hour == 12)
            {
                hour = 0;
            }
            DateTime timeNow = DateTime.Now;
            DateTime d = new DateTime(timeNow.Year, timeNow.Month, timeNow.Day, hour, minute, 0);
            long interval = (d.Ticks - timeNow.Ticks) / 10000; // in miliseconds
            if (interval < 1)
            {
                const long millasecondPerSecond = 1000;
                const long secondsPerDay = 60 * 60 * 24;
                interval += millasecondPerSecond * secondsPerDay;
            }
            return (int)interval;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            
            player.SoundLocation = "trapical_alarm.wav";
            player.PlayLooping();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void alarm_Load(object sender, EventArgs e)
        {


            // Dock the control to the top of the form.
            minuteUpDown.Top = 44;
            minuteUpDown.Left = 151;
            minuteUpDown.Width = 38;

            // Set the Minimum, Maximum, and initial Value.
            minuteUpDown.Maximum = 59;
            minuteUpDown.Minimum = 0;

            minuteUpDown.ReadOnly = true;

            // Add the NumericUpDown to the Form.
            Controls.Add(minuteUpDown);
        }


    }
}
